package com.example.rightmost.service.Impl;

import com.example.rightmost.dao.UserMapper;
import com.example.rightmost.entity.Result;
import com.example.rightmost.entity.User;
import com.example.rightmost.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author:javayhch
 * @date:2024/8/17
 * @description:
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    public UserMapper userMapper;

    @Override
    public Result reg(User user) {
        if (user == null){
            return Result.error("不能为空");
        }
        User getUser = userMapper.selectUser(user);
        if (getUser == null){
            user.setCreateTime(LocalDateTime.now());
            userMapper.insertUser(user);
            return Result.success();
        } else {
            return Result.error("此用户已存在");
        }
    }

    @Override
    public Result login(User user) {
        User getUser = userMapper.selectUser(user);
        if (getUser == null){
            return Result.error("用户不存在");
        } else if (getUser.getStatus() != '1'){
            return Result.error("账号异常，无法登录");
        } else if (user.getPassword().equals(getUser.getPassword())){
            return Result.success(getUser);
        } else {
            return Result.error("密码错误");
        }
    }

    @Override
    public Result adminLogin(User user) {
        User getUser = userMapper.selectUser(user);
        if (getUser == null){
            return Result.error("不存在该账号");
        } else if (getUser.getRole() != 1){
            return Result.error("您不是管理员");
        } else if (user.getPassword().equals(getUser.getPassword())){
            return Result.success(getUser);
        } else {
            return Result.error("密码错误");
        }
    }

    @Override
    public Result getInfoById(Integer id) {
        User getUser = userMapper.selectUserById(id);
        return Result.success(getUser);
    }

    @Override
    public Result updateInfo(User user) {
        user.setLastUpdateTime(LocalDateTime.now());
        int i = userMapper.updateInfo(user);
        if(i > 0){
            return Result.success();
        }else{
            return Result.error("修改失败");
        }
    }

    @Override
    public List<User> getUserList(User user) {
        return userMapper.getUserList(user);
    }

    @Override
    public Result deleteUser(Integer id) {
        int i = userMapper.deleteUser(id);
        if(i > 0){
            return Result.success();
        }else{
            return Result.error("删除失败");
        }
    }

    @Override
    public Result updateStatus(Integer id) {
        int i = userMapper.updateStatus(id);
        if(i > 0){
            return Result.success();
        }else{
            return Result.error("修改失败");
        }
    }


}
